package my.algorithms.mcorrea.topcoder;

import java.util.*;

/**
 * http://community.topcoder.com/stat?c=problem_statement&pm=11740
 *
 * @author mcorrea
 */
public class KingSort {

	public static void main(String[] args) {
		for( String x: getSortedList(new String[]{"Louis IX", "Louis VIII"}) ){
			System.out.println(x);
		}
		System.out.println("---------------");	
		for( String x: getSortedList(new String[]{"Louis IX", "Philippe II"}) ){
			System.out.println(x);
		}
		System.out.println("---------------");	
		for( String x: getSortedList(new String[]{"Richard III", "Richard I", "Richard II"}) ){
			System.out.println(x);
		}
		System.out.println("---------------");	
		for( String x: getSortedList(new String[]{"John X", "John I", "John L", "John V"}) ){
			System.out.println(x);
		}
		System.out.println("---------------");		
		for( String x: getSortedList(new String[]{"Philippe VI", "Jean II", "Charles V", "Charles VI", "Charles VII", "Louis XI"}) ){
			System.out.println(x);
		}
		
		System.out.println("---------------");		
		String[] result = new String[]{"Bob XV", "Bruce VIII", "Bruce XVIII", "Charles V", "Charles XXVIII", "Charles XXXIII", "Derek VIII", "Derek XXX", "Derek L", "Edward XLVI", "Elizabeth X", "Elizabeth XLIX", "Fero XIV", "Fero XXII", "Genadij IV", "Genadij VI", "Genadij XVII", "Genadij XXVIII", "James XXXIII", "James XLI", "Luka XX", "Luka XXVIII", "Lukas XLVI", "Marek XXIV", "Maru II", "Maru XIX", "Maru XXII", "Pawel XV", "Pawel XX", "Pawel XXXIII", "Petrzlen VI", "Petrzlen XXXII", "Petrzlen XXXIV", "Philip XLVIII", "William II", "William IX", "William XX", "William XXXV", "William XLI", "William XLIII", "William XLV", "Zemco IV", "Zemco V", "Zemco XVII", "Zemco XIX", "Zemco XXVIII", "Zemco XXXII", "Zemco XLV"};
		int k=0; boolean pass=true;
		for( String x: getSortedList(new String[]{"Edward XLVI", "Zemco XXVIII", "Pawel XX", "Pawel XV", "Petrzlen VI", "Fero XXII", "Zemco IV", "Genadij VI", "Charles XXVIII", "Maru XIX", "Elizabeth X", "William XX", "Zemco XVII", "Zemco XLV", "Bruce XVIII", "James XLI", "William XXXV", "Bob XV", "Genadij IV", "Derek L", "Charles XXXIII", "Zemco XXXII", "Luka XXVIII", "William XLV", "Petrzlen XXXIV", "Petrzlen XXXII", "William II", "Fero XIV", "Zemco V", "William XLI", "Genadij XVII", "William XLIII", "Luka XX", "Maru XXII", "Lukas XLVI", "Philip XLVIII", "Derek VIII", "Pawel XXXIII", "James XXXIII", "Charles V", "Marek XXIV", "Zemco XIX", "Maru II", "Bruce VIII", "Derek XXX", "William IX", "Genadij XXVIII", "Elizabeth XLIX"}) ){
			pass &= x.equals(result[k++]);
		}
		System.out.println(pass);
		
	}

	public static String[] getSortedList(String[] kings) {

		Map<String, String> namesMap = new TreeMap<String, String>( new Comparator<String>(){

			@Override
			public int compare(String o1, String o2) {
				String[] name1 =o1.split(" ");
				String[] name2 = o2.split(" ");
				if(!name1[0].equals(name2[0])){
					return name1[0].compareTo(name2[0]);
				}
				return Integer.parseInt(name1[1]) - Integer.parseInt(name2[1]);
				
			}
		});
		for (String king : kings) {
			String[] name = king.split("\\s");
			//conver to decimal from roman
			int number = 0;
			char prev=' ';
			for(char c: name[1].toCharArray()){
				switch(c){
					case 'I':
						number +=1;
					break;
					case 'V':
						if(prev!='I')number +=5;
						else number += 3;
					break;
					case 'X':
						if(prev!='I') number+=10;
						else number +=8;
					break;
					case 'L':
						if(prev!='X')number +=50;
						else number += 30;
				}
				prev = c;
			}
			namesMap.put(name[0]+" "+number, king);
		}
		
		String[] result = new String[kings.length];
		int k =0;
		for(String n: namesMap.keySet()){
			result[k++] = namesMap.get(n);
		}
		return result;
	}
}
